package com.luoqiz.security.config;

import com.luoqiz.security.service.MyUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private MyUserDetailsService userDetailsService;

    @Autowired
    private WebResponseExceptionTranslator bootWebResponseExceptionTranslator;

    //	定义令牌端点上的安全性约束
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients().tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }

    //	定义客户端详细信息服务的配置器。
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("testclient")
//                .secret(PasswordEncoderFactories.createDelegatingPasswordEncoder().encode("testclient"))
                .secret(passwordEncoder.encode("testclient"))
                .accessTokenValiditySeconds(1200)
                .authorizedGrantTypes("authorization_code", "refresh_token", "password","client_credentials","implicit")
                .refreshTokenValiditySeconds(3600).scopes("all", "read", "write").redirectUris("http://www.baidu.com")
        .and().withClient("boya")
                .secret(passwordEncoder.encode("boya-secret"))
                .accessTokenValiditySeconds(1200)
                .authorizedGrantTypes("authorization_code", "refresh_token", "password","client_credentials","implicit")
                .refreshTokenValiditySeconds(3600).scopes("all", "read", "write").redirectUris("http://www.baidu.com");
    }

    //	定义授权和令牌端点以及令牌服务。
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
                .tokenStore(memoryTokenStore())
                .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
        endpoints.exceptionTranslator(bootWebResponseExceptionTranslator);
    }

    // 使用最基本的InMemoryTokenStore生成token
    @Bean
    public TokenStore memoryTokenStore() {
        return new InMemoryTokenStore();
    }
}
